package com.thinkingstar.iads.datacenter.entity.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.jsonUtils.AjaxResult;
import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.CommonDateUtil;
import com.thinkingstar.iads.datacenter.entity.DcCarmaintenanceFacility;
import com.thinkingstar.iads.datacenter.entity.DcTemplateVersion;

@IocBean(args = {"refer:dao"})
public class DcCarmaintenanceFacilityDao extends EntityRepositoryServiceImpl<DcCarmaintenanceFacility>{

	public DcCarmaintenanceFacilityDao(Dao dao) {
		super(dao);
	}
	
	
	public QueryResult findCarmaintenanceFacility(NutMap param){
		List <DcCarmaintenanceFacility> list = new ArrayList<>();
		Pager pager = null;
		Criteria criteria = Cnd.NEW();
		if(StringUtils.isNotBlank(param.getString("type"))){
			criteria.where().and("type", "=", param.getString("type"));
		}
		criteria.getOrderBy().asc("create_time");
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"),param.getInt("page_size"));
			pager.setRecordCount(count(criteria));
			list = query(criteria, pager);
		}else{
			list = query(criteria);
		}
		return new QueryResult(list, pager);
	}
	
	//更新机务设施数据库设施相亲
	public AjaxResult updateFacilityInfo(NutMap param){
		DcCarmaintenanceFacility update_obj = fetch(param.getString("id"));
		update_obj.setFacility_info(param.getString("facility_info"));
		return update(update_obj)==1?AjaxResult.success("更新成功!"):AjaxResult.error("更新失败!");
	}
	
	//更新机务设施数据库
	public AjaxResult updateFacility(DcCarmaintenanceFacility data){
		if(data == null){
			return AjaxResult.error("更新失败!");
		}
		String id = data.getId();
		if(StringUtils.isNotBlank(id)){
			DcCarmaintenanceFacility update_obj = fetch(id);
			update_obj.setCity(data.getCity());
			update_obj.setScale(data.getScale());
			update_obj.setRailway(data.getRailway());
			update_obj.setNature(data.getNature());
			update_obj.setFacilities_name(data.getFacilities_name());
			update_obj.setLongitude(data.getLongitude());
			update_obj.setDimensionality(data.getDimensionality());
			update_obj.setCreate_time(CommonDateUtil.getDate());
			return update(update_obj)==1?AjaxResult.success("更新成功!"):AjaxResult.error("更新失败!");
		}else{
			return insert(data)!=null?AjaxResult.success("更新成功!"):AjaxResult.error("更新失败!");
		}
	}

}
